﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public class TextBox : SuperControl
	{
		Label textLabel;
		Pointer pointer;
		Theme.TextBox theme;

		int leftPadding;
		int rightPadding;
		int topPadding;
		Vector2 size;

		public override Vector2 Size
		{
			get {return size; }
		}

		public string Text
		{
			get { return textLabel.Text; }
			set { textLabel.Text = value; }
		}

		public int TextHeight
		{
			get { return TextTools.Height("abcdefghijklmnopqrstuvwxyz", theme.textFont); }
		}

		public TextBox(object parent, InputHandler inputhandler, string name, Vector2 position, int width, Theme.TextBox theme)
			: base(name, parent, position)
		{
			controls = new ControlManager(this);
			this.theme = theme;

			int pointerHeight = theme.pointerTheme.pointerTop.Height + theme.pointerTheme.pointerBottom.Height + TextHeight;
			int textBoxHeight = (theme.borderTheme.bordertop.Height + theme.borderTheme.borderbottom.Height) * 2 + pointerHeight;
			size = new Vector2(width, textBoxHeight);

			size = AutoSizeTools.AdjustSizeToTextures(size,theme.borderTheme,theme.background);

			AddBorder();
			AddBackground();
			SetPadding();
			AddLabel();
			Vector2 pointerPosition = position + new Vector2(0, theme.borderTheme.bordertop.Height * 2);
			pointer = controls.AddPointer("", pointerPosition, pointerHeight, theme.pointerTheme);
			SetPointerPosition();

			inputhandler.KeyboardPressed += OnKeyPressed;
		}

		public TextBox(object parent, InputHandler inputhandler, string name, Vector2 position, Vector2 size, Theme.TextBox theme)
			: base(name,parent,position)
		{
			controls = new ControlManager(this);
			this.theme = theme;

			this.size = size;
						
			AddBorder();
			AddBackground();
			SetPadding();
			AddLabel();
			Vector2 pointerPosition = position + new Vector2(0, theme.borderTheme.bordertop.Height * 2);
			int pointerHeight = (int)size.Y - ((theme.borderTheme.bordertop.Height + theme.borderTheme.borderbottom.Height) * 2);
			pointer = controls.AddPointer("", pointerPosition, pointerHeight, theme.pointerTheme);
			SetPointerPosition();

			inputhandler.KeyboardPressed += OnKeyPressed;
		}

		#region Setup Controls
		public void AddBorder()
		{
			controls.AddBorder("", WindowPosition, size, Border.Sides.All, theme.borderTheme);
		}

		public void AddBackground()
		{
			Vector2 backgroundPosition = relativePosition + new Vector2(theme.borderTheme.borderleft.Width, theme.borderTheme.bordertop.Height);
			Vector2 backgroundSize = size - new Vector2(theme.borderTheme.borderleft.Width + theme.borderTheme.borderright.Width, theme.borderTheme.bordertop.Height + theme.borderTheme.borderbottom.Height);
			controls.AddTileImage("", backgroundPosition, theme.background, backgroundSize);
		}

		public void AddLabel()
		{
			int labelPositionX = (int)relativePosition.X + leftPadding;
			int labelPositionY = (int)relativePosition.Y + topPadding;
			Vector2 labelPosition = new Vector2(labelPositionX, labelPositionY);
			textLabel = controls.AddLabel("", labelPosition, "", theme.textFont);
		}

		public void SetPadding()
		{
			leftPadding = theme.borderTheme.borderleft.Width * 2;
			rightPadding = theme.borderTheme.borderright.Width * 2;
			topPadding = theme.borderTheme.bordertop.Height + (int)(size.Y - (TextHeight + theme.borderTheme.bordertop.Height + theme.pointerTheme.pointerTop.Height)) / 2;
		}
		#endregion

		public void SetPointerPosition()
		{
			Vector2 pointerPosition = pointer.ParentPosition;
			pointerPosition.X = theme.borderTheme.borderleft.Width * 2 + textLabel.Width;
			pointer.ParentPosition = pointerPosition;
		}

		internal override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
		{
			if (Visible)
			{
				controls.Draw(spriteBatch);
			}
		}

		internal override bool OnLeftClick(MouseEventArgs e)
		{
			if (IsWithinBounds(e.Position) && Enabled)
			{
				hasFocus = true;
				pointer.HasFocus = true;
				
				return true;
			}
			hasFocus = false;
			pointer.HasFocus = false;
			return false;
		}

		private void OnKeyPressed(object sender, KeyPressedEventArgs e)
		{
			if (hasFocus)
			{
				if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.Back)
				{
					int textLength = textLabel.Text.Length;
					if (textLength > 0)
					{
						textLabel.Text = textLabel.Text.Remove(textLength - 1);
						SetPointerPosition();
					}
				}
				else if(TextTools.IsChar(e.KeyPressed.ToString()))
				{
					string newText = textLabel.Text + e.KeyPressed;
					if (TextTools.Width(newText, theme.textFont) <= size.X - leftPadding - rightPadding)
					{
						textLabel.Text = newText;
						SetPointerPosition();
					}
				}
			}
		}
	}
}
